package com.example.first.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.first.R;
import com.example.first.modle.AddressBook;
import com.example.first.modle.ApiResponse;
import com.example.first.services.ApiService;
import com.example.first.utils.RetrofitClient;

import retrofit2.Call;

public class AddressEditActivity extends AppCompatActivity {

    public  AddressBook addressBook;

    private ImageView back;
    private EditText name;
    private EditText phone;
    private EditText detail;
    private TextView local;
    //保存按钮
    private Button save;
    //删除按钮
    private Button delete;


    private Intent intent;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_edit);


        // 设置 Toolbar 标题
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("外卖订餐");
        }


        // 设置状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor("#FFC300")); // 设置为你想要的颜色
        }


        //返回
        back = findViewById(R.id.back);
        back.setOnClickListener(v -> finish());

        intent=getIntent();
        addressBook=(AddressBook) intent.getSerializableExtra("address");

        name=findViewById(R.id.name);
        phone=findViewById(R.id.phone);
        detail=findViewById(R.id.detail);
        local=findViewById(R.id.address);


        if(addressBook!=null){
            name.setText(addressBook.getConsignee());
            phone.setText(addressBook.getPhone());
            detail.setText(addressBook.getDetail());
            local.setText(addressBook.getLocal());
        }


        save=findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String S_name=name.getText().toString();
                String S_phone=phone.getText().toString();
                String S_detail=detail.getText().toString();

                if(S_name.isEmpty()||S_phone.isEmpty()||S_detail.isEmpty()){
                    Toast.makeText(AddressEditActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                    return;
                }

                UpdateOrSave(S_name,S_phone,S_detail);


            }
        });



    }

    private void UpdateOrSave(String sName, String sPhone, String sDetail) {
        if(addressBook==null){
            //新建
            AddressBook ad=new AddressBook();
            ad.setConsignee(sName);
            ad.setPhone(sPhone);
            ad.setDetail(sDetail);
            ad.setProvinceCode("61");
            ad.setProvinceName("陕西省");
            ad.setCityCode("6101");
            ad.setCityName("西安市");
            ad.setDistrictCode("610118");
            ad.setDistrictName("鄠邑区");
            ad.setLabel("0");
            ad.setIsDefault(0);

            ApiService apiService = RetrofitClient.getApiService();
            Call<ApiResponse<String>> call = apiService.addAddress(ad);
            call.enqueue(new retrofit2.Callback<ApiResponse<String>>() {
                @Override
                public void onResponse(Call<ApiResponse<String>> call, retrofit2.Response<ApiResponse<String>> response) {
                    if (response.isSuccessful()&& response.body() != null)
                    {
                        if (response.body().getCode() == 1)
                        {
                            Toast.makeText(AddressEditActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }else {
                            Toast.makeText(AddressEditActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(AddressEditActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                    //失败
                    Toast.makeText(AddressEditActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    Toast.makeText(AddressEditActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                }
            });

        }else {
            //修改
            AddressBook ad=new AddressBook();
            ad.setId(addressBook.getId());
            ad.setConsignee(sName);
            ad.setPhone(sPhone);
            ad.setDetail(sDetail);

            ApiService apiService = RetrofitClient.getApiService();
            Call<ApiResponse<String>> call = apiService.updateAddress(ad);
            call.enqueue(new retrofit2.Callback<ApiResponse<String>>() {
                @Override
                public void onResponse(Call<ApiResponse<String>> call, retrofit2.Response<ApiResponse<String>> response) {
                    if (response.isSuccessful()&& response.body() != null)
                    {
                        if (response.body().getCode() == 1)
                        {
                            Toast.makeText(AddressEditActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }else {
                            Toast.makeText(AddressEditActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT);
                        }
                    }else {
                        Toast.makeText(AddressEditActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                    //失败
                    Toast.makeText(AddressEditActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
